<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="close"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds">
      <!-- 多选的选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
      <!-- <el-checkbox label="113">开发者</el-checkbox>
      <el-checkbox label="115">人事</el-checkbox> -->
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="assignRoles">
        确定
      </el-button>
      <el-button size="small" @click="$emit('update:showRoleDialog', false)">
        取消
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      list: [], // 角色列表
      roleIds: [], // 选中角色列表
      emplId: '' // 当前点击员工的id
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {

    // 弹层关闭执行
    close () {
      // console.log(123)
      this.$emit('update:showRoleDialog', false)
      this.roleIds = []
    },

    // 获取角色列表数据
    async getRoleList () {
      /**
       * 1.调接口
       * 2.存数据
       */
      const { rows } = await getRoleList({ page: 1, pagesize: 100 })
      this.list = rows
      // console.log('员工列表', rows)
    },

    // 获取当前员工角色 ==> 回显数据
    async getUserDetailById (id) {
      // console.log('当前点击的员工id: ', id)
      /**
       * 1.存储当前点击员工id => 后面用
       * 2.调接口回显数据
       */
      this.emplId = id
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds
      // console.log(this.roleIds)
    },

    // 给员工分配角色
    async assignRoles () {
      /**
       * 1.调用接口(参数为数组, id: 当前员工ID roleIds: 数组)
       * 2.提示成功
       * 3.关闭弹层
       */
      await assignRoles({ id: this.emplId, roleIds: this.roleIds })
      this.$message.success('分配角色成功')
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style scoped>
.el-checkbox {
  margin-bottom: 20px;
}
</style>
